﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace RotateDemo01
{
    /// <summary>
    /// RotateButtonByAnimationWindow.xaml 的交互逻辑
    /// </summary>
    public partial class RotateButtonByAnimationWindow : Window
    {
        private DoubleAnimation _rotateAnimation;
        public RotateButtonByAnimationWindow()
        {
            InitializeComponent();
            SetupAnimation();
        }

        private void SetupAnimation()
        {
            if (_rotateAnimation != null)
            {
                _rotateAnimation = (DoubleAnimation)((Storyboard)rotateButton.Resources["rotationStoryboard"]).Children[0];
                UpdateAnimationDuration();
            }
        }

        private void SpeedSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            UpdateAnimationDuration();
        }

        private void UpdateAnimationDuration()
        {
            double speed = speedSlider.Value;
            if (_rotateAnimation != null)
            {
                _rotateAnimation.Duration = TimeSpan.FromSeconds(10 / speed); // 反比例关系

                // 重新启动动画以应用新的持续时间
                buttonRotationTransform.BeginAnimation(RotateTransform.AngleProperty, null); // 停止当前动画
                buttonRotationTransform.BeginAnimation(RotateTransform.AngleProperty, _rotateAnimation); // 启动新的动画
            }
        }
    }
}
